
var map2D;
(function (map2D) {
    var Barrier = /** @class */ (function () {
        function Barrier() {
        }
        /**设置障碍信息
         * @param barrierArray 障碍配置数组
         * @param mapSize 地图尺寸 [最小x,最小y,最大x,最大y,障碍列数，障碍行数]
         * @param gridSize 格子宽度
        */
        Barrier.prototype.setBarrier = function (barrierArray, mapSize, gridSize) {
            var s = this;
            s._barriers = barrierArray;
            s._minX = mapSize[0];
            s._minY = mapSize[1];
            s._maxX = mapSize[2];
            s._maxY = mapSize[3];
            s._col = mapSize[4];
            s._row = mapSize[5];
            s._minCol = Math.floor(s._minX / s._gridSize) % s._col;
            s._minRow = Math.floor(s._minY / s._gridSize) % s._row;
            s._gridSize = gridSize;
        };
        /**判断障碍状态
         * @param x 世界坐标x
         * @param y 世界坐标y
         * @param flag 通行标志 参考Barrier常量 1 陆 2 水 4 空
         * @return 返回障碍状态 头3位存在大于0则为通行，第4位代表是否有阴影
        */
        Barrier.prototype.barrierState = function (x, y, flag) {
            if (flag === void 0) { flag = 1; }
            var s = this;
            var col, row, value;
            if (x < s._minX || y < s._minY || x > s._maxX || y > s._maxY)
                return 0;
            col = Math.floor(x / s._gridSize) % s._col;
            row = Math.floor(y / s._gridSize) % s._row;
            if (col < 0)
                col += s._col;
            if (row < 0)
                row += s._row;
            value = s._barriers[col + row * s._col];
            if (value == null)
                return Barrier.PASS;
            return value & flag;
        };
        /**判断障碍状态(根据格子索引)
         * @param col 格子列索引
         * @param row 格子行索引
         * @param flag 通行标志 参考Barrier常量 1 陆 2 水 4 空
         * @return 返回障碍状态 头3位存在大于0则为通行，第4位代表是否有阴影
        */
        Barrier.prototype.barrierGridState = function (col, row, flag) {
            if (flag === void 0) { flag = 1; }
            var s = this;
            var value;
            if (col < s._minCol || row < s._minRow || col > s._col || row > s._row)
                return 0;
            if (col < 0)
                col += s._col;
            if (row < 0)
                row += s._row;
            value = s._barriers[col + row * s._col];
            if (value == null)
                return Barrier.PASS;
            return value & flag;
        };
        Barrier.prototype.outputJson = function () {
            var i, j, len, len2, ind;
            var s = this;
            len = s._row;
            len2 = s._col;
            for (i = 0; i < len; ++i) {
                for (j = 0; j < len2; ++j) {
                    ind = j + i * s._col;
                    s._barriers[ind] = s._barriers[ind] === undefined ? Barrier.LAND : s._barriers[ind];
                }
            }
            return s._barriers;
        };
        /**不通行*/ Barrier.NONE = 0;
        /**陆路通行*/ Barrier.LAND = 1;
        /**水路通行*/ Barrier.WATER = 2;
        /**空路通行*/ Barrier.AIR = 4;
        /**阴影通行*/ Barrier.SHADOW = 8;
        /**全通行*/ Barrier.PASS = 7;
        return Barrier;
    }());
    map2D.Barrier = Barrier;
})(map2D || (map2D = {}));
__reflect(map2D.Barrier.prototype, "map2D.Barrier");

var map2D;
(function (map2D) {
    var DirectionMode = /** @class */ (function () {
        function DirectionMode() {
        }
        /**八个方向*/ DirectionMode.EIGHT = 1;
        /**四个方向*/ DirectionMode.FOUR = 2;
        return DirectionMode;
    }());
    map2D.DirectionMode = DirectionMode;
})(map2D || (map2D = {}));
__reflect(map2D.DirectionMode.prototype, "map2D.DirectionMode");

var map2D;
(function (map2D) {
    var DirectionType = /** @class */ (function () {
        function DirectionType() {
        }
        /**获取角度对应的状态
         * @param agl 弧度值
         * @param directionMode 方向模式 1 8个方向 2 4个方向 参考DirectionMode常量
         * @param initDir 初始角度
         * @return 状态值	参考DirectionType常量
        */
        DirectionType.getDirectorSts = function (agl, directionMode, initDir) {
            if (directionMode === void 0) { directionMode = 1; }
            if (initDir === void 0) { initDir = 0; }
            if (agl < 0)
                agl += GYLite.MathConst.DOUBLE_PI;
            agl = agl % GYLite.MathConst.DOUBLE_PI;
            var dir;
            var pi_p;
            if (directionMode == 1) {
                pi_p = Math.PI / 8;
                if (agl < Math.PI / 8 || agl > pi_p * 15)
                    dir = DirectionType.RIGHT;
                else if (agl < pi_p * 3)
                    dir = DirectionType.RIGHT_DOWN;
                else if (agl < pi_p * 5)
                    dir = DirectionType.DOWN;
                else if (agl < pi_p * 7)
                    dir = DirectionType.LEFT_DOWN;
                else if (agl < pi_p * 9)
                    dir = DirectionType.LEFT;
                else if (agl < pi_p * 11)
                    dir = DirectionType.LEFT_UP;
                else if (agl < pi_p * 13)
                    dir = DirectionType.UP;
                else if (agl <= pi_p * 15)
                    dir = DirectionType.RIGHT_UP;
            }
            else if (directionMode == 2) {
                pi_p = Math.PI / 4;
                if (agl < Math.PI / 4 || agl > pi_p * 7)
                    dir = DirectionType.RIGHT;
                else if (agl < pi_p * 3)
                    dir = DirectionType.DOWN;
                else if (agl < pi_p * 5)
                    dir = DirectionType.LEFT;
                else if (agl <= pi_p * 7)
                    dir = DirectionType.UP;
            }
            return dir + initDir;
        };
        /**获取状态对应的角度
         * @param dir
         * @param initDir 初始角度
         * @return 状态值	参考DirectionType常量
        */
        DirectionType.getDirectorAngle = function (dir) {
            return DirectionType.angleArray[dir];
        };
        DirectionType.NONE = -1;
        DirectionType.UP = 0;
        DirectionType.RIGHT_UP = 1;
        DirectionType.RIGHT = 2;
        DirectionType.RIGHT_DOWN = 3;
        DirectionType.DOWN = 4;
        DirectionType.LEFT_DOWN = 5;
        DirectionType.LEFT = 6;
        DirectionType.LEFT_UP = 7;
        DirectionType.angleArray = [
            -Math.PI / 2,
            -Math.PI / 4,
            0,
            Math.PI / 4,
            Math.PI / 2,
            Math.PI / 4 * 3,
            Math.PI,
            -Math.PI / 4 * 3
        ];
        return DirectionType;
    }());
    map2D.DirectionType = DirectionType;
})(map2D || (map2D = {}));
__reflect(map2D.DirectionType.prototype, "map2D.DirectionType");

var map2D;
(function (map2D) {
    var MapBricker = /** @class */ (function () {
        function MapBricker(sceneLayer) {
            var s = this;
            s._sceneLay = sceneLayer;
            // s._brickArray = {};
            s._matrix = new egret.Matrix;
        }
        MapBricker.prototype.setSceneSize = function (w, h) {
            var s = this;
            s._sceneW = w;
            s._sceneH = h;
        };
        MapBricker.prototype.clearBricks = function () {
            var s = this;
            // for(var key in s._brickArray)
            // {
            // 	s._brickArray[key].graphics.clear();
            // 	// s._brickArray[key].clearBitmapFill();
            // }
            s._sceneLay.graphics.clear();
        };
        MapBricker.prototype.brickDraw = function (x, y, innerCol, innerRow, aliasTex, aliasId, areaSize) {
            if (areaSize === void 0) { areaSize = 512; }
            var s = this;
            // let brick:Brick;
            if (aliasTex == null)
                return;
            // if(s._brickArray[aliasId] == null)
            // {
            // 	brick = s._brickArray[aliasId] = new Brick();						
            // 	s._sceneLay.addChild(brick);
            // }
            // else
            // 	brick = s._brickArray[aliasId];			
            var tx, ty, drawX, drawY, drawW, drawH;
            tx = x;
            ty = y;
            s._matrix.tx = (-innerCol * areaSize | 0) + tx;
            s._matrix.ty = (-innerRow * areaSize | 0) + ty;
            var g;
            // g = <GYLite.GYGraphics>brick.graphics;			
            g = s._sceneLay.graphics;
            drawX = tx < 0 ? 0 : tx;
            drawY = ty < 0 ? 0 : ty;
            drawW = tx < 0 ? areaSize + tx : (tx + areaSize > s._sceneW ? s._sceneW - tx : areaSize);
            drawH = ty < 0 ? areaSize + ty : (ty + areaSize > s._sceneH ? s._sceneH - ty : areaSize);
            if (drawW == 0 || drawH == 0)
                return;
            g.beginBitmapFill(aliasTex, s._matrix, false);
            if (drawX + drawW - s._matrix.tx > aliasTex.$bitmapWidth)
                drawW = aliasTex.$bitmapWidth - drawX;
            if (drawY + drawH - s._matrix.ty > aliasTex.$bitmapHeight)
                drawH = aliasTex.$bitmapHeight - drawY;
            g.drawRect(drawX, drawY, drawW, drawH);
            g.endFill();
        };
        return MapBricker;
    }());
    map2D.MapBricker = MapBricker;
    // export class Brick extends GYLite.GYSprite
    // {
    // }
    // export class Brick extends egret.Bitmap{
    // 	private _renderMatrix:egret.Matrix;
    // 	public constructor(value:egret.Texture = null,rect:GYLite.Scale9GridRect=null)
    // 	{
    // 		super(value);
    // 		let s = this;
    // 		s.$renderNode = new egret.sys.BitmapNode;						
    // 	}
    // 	public beginBitmapFill(m:egret.Matrix, r:boolean):void
    // 	{
    // 		var s = this;
    // 		var node:any;			
    // 		node = s.$renderNode;
    // 		node.GYFill = 1;//位图填充标志
    // 		node.image = s.texture;
    // 		node.imageHeight = s.texture.$sourceWidth;
    // 		node.imageWidth = s.texture.$sourceHeight;
    // 		node.smoothing = s.smoothing;
    // 		s._renderMatrix = m;			
    // 	}
    // 	public drawRect(tX:number, tY:number, w:number, h:number):void
    // 	{
    // 		var s = this;
    // 		var node:any = s.$renderNode;
    // 		var sclX:number,sclY:number;					
    // 		sclX = s._renderMatrix.a;
    // 		sclY = s._renderMatrix.d;
    // 		node.drawData.push(s.texture.$bitmapX - s._renderMatrix.tx + tX, s.texture.$bitmapY - s._renderMatrix.ty + tY, w/sclX, h/sclY, s.texture.$offsetX + tX, s.texture.$offsetY + tY, w, h);
    // 		++node.renderCount;						
    // 		s.$renderDirty = true;
    // 	}
    // 	public clearBitmapFill():void
    // 	{
    // 		var s = this;
    // 		s.$renderNode.cleanBeforeRender();
    // 	}
    // }
})(map2D || (map2D = {}));
__reflect(map2D.MapBricker.prototype, "map2D.MapBricker");

var map2D;
(function (map2D) {
    var Default = /** @class */ (function () {
        function Default() {
            /**镜头拉伸高度判断*/ this.scale_height = [600, 0.6, 900, 0.65, 1200, 0.55, 1500, 0.5, 1800, 0.4];
            /**镜头拉伸速度判断*/ this.scale_speed = [15, 0.6, 30, 0.5, 50, 0.4];
            /**镜头推动步长*/ this.scale_step = 0.005;
            /**镜头初始缩放值*/ this.scale_init = 0.8;
            /**镜头保持时间(毫秒)*/ this.scale_keep_time = 2000;
            /**奔跑宽度比例左边界*/ this.run_width_p = 0.45;
            /**奔跑高度比例上边界*/ this.run_height_p = 0.6;
            /**奔跑宽度比例右边界*/ this.run_width_p2 = 0.65;
            /**奔跑高度比例下边界*/ this.run_height_p2 = 0.65;
            /**角色初始X*/ this.role_init_x = 380;
            /**角色初始Y*/ this.role_init_y = 680;
            /**地图初始X*/ this.map_init_x = 0;
            /**地图初始Y*/ this.map_init_y = 0;
            /**地图边界最小X(像素)*/ this.map_min_x = 0;
            /**地图边界最小Y(像素)*/ this.map_min_y = 0;
            /**地图边界最大X(像素)*/ this.map_max_x = 4096;
            /**地图边界最大Y(像素)*/ this.map_max_y = 1536;
            /**背景模式*/ this.background_type = 2;
            /**背景移动速度*/ this.backMove_speed = -0.1;
            /**尽头x区*/ this.max_area_x = Number.MAX_VALUE;
            /**尽头y区*/ this.max_area_y = Number.MAX_VALUE;
            /**背景缩放比例X*/ this.back_scale_x = 1;
            /**背景缩放比例Y*/ this.back_scale_y = 1;
            /**地图区块尺寸*/ this.map_areasize = 512;
            /**一格大小*/ this.grid_size = 32;
            /**默认奔跑速度(每秒/像素)*/ this.move_speed = 120;
        }
        return Default;
    }());
    map2D.Default = Default;
})(map2D || (map2D = {}));
__reflect(map2D.Default.prototype, "map2D.Default");



var map2D;
(function (map2D) {
    var SceneManagerBase = /** @class */ (function () {
        function SceneManagerBase(gameContainer, sceneLayer, sceneBaseWidth, sceneBaseHeight) {
            this._mapPCol = 4;
            this._mapPRow = 4;
            var s = this;
            s._sceneRoleVec = [];
            s._delRoleVec = [];
            s._depthArr = [];
            s._yIndexTime = 0;
            s._runRect = new egret.Rectangle(400, 300, 300, 300);
            s._visibleRect = new egret.Rectangle;
            s._mapMinX = -Number.MAX_VALUE;
            s._mapMaxX = Number.MAX_VALUE;
            s._mapMinY = -Number.MAX_VALUE;
            s._mapMaxY = Number.MAX_VALUE;
            s._areaArr = [];
            s._areaYArr = [];
            s._mapMoveListener = new GYLite.Listener;
            s._sceneRoleStsDict = {};
            s._sceneScale = 1;
            s._gameContainer = gameContainer;
            s._sceneLayer = sceneLayer;
            s._targetScale = 0;
            s._scaleStep = 0.005;
            s._scaleKeepTime = 0.8;
            s._sceneTouchabled = true;
            s.barrier = new map2D.Barrier;
            s._mapBricker = new map2D.MapBricker(s._sceneLayer);
            s._mapBackBricker = new map2D.MapBricker(s._sceneLayer);
            s.setSceneSize(sceneBaseWidth, sceneBaseHeight, 1);
            GYLite.GYKeyboard.getInstance().removeKeyListener(this);
        }
        /**获取默认配置，覆盖可重新写返回的默认配置，返回对象参考Default*/
        SceneManagerBase.prototype.getDefault = function () {
            var s = this;
            if (s._default == null)
                s._default = new map2D.Default;
            return s._default;
        };
        SceneManagerBase.prototype.setMyPlayer = function (player) {
            var s = this;
            s._myPlayer = player;
        };
        SceneManagerBase.prototype.setSceneSize = function (w, h, scale) {
            if (scale === void 0) { scale = 1; }
            var s = this;
            s._sceneLayer.width = s._sceneBaseWidth = w;
            s._sceneLayer.height = s._sceneBaseHeight = h;
            s._mapBackBricker.setSceneSize(w, h);
            s._mapBricker.setSceneSize(w, h);
            s.setScale(scale, false);
            s.setMapXY(s._mapX, s._mapY, true);
        };
        SceneManagerBase.prototype.setMapXY = function (toX, toY, reset) {
            if (reset === void 0) { reset = false; }
            var s = this;
            if (s._mapX == toX && s._mapY == toY && !reset)
                return;
            s._mapX = (toX >> 0);
            s._mapY = (toY >> 0);
            s._mapRight = s._mapX + s._sceneWidth + 100;
            s._mapBottom = s._mapY + s._sceneHeight + 100;
            // s._gameManager.gameWorld.sceneX = s._mapX * s._sceneScale;
            // s._gameManager.gameWorld.sceneY = s._mapY * s._sceneScale;
            s.setSceneElement(s._mapX, s._mapX + s._sceneWidth, s._mapY, s._mapY + s._sceneHeight, reset);
            if (s._isScaleChange) {
                var m = s._gameContainer.matrix;
                m.a = m.d = s._sceneScale;
                s._gameContainer.matrix = m;
                s._isScaleChange = false;
            }
            s._mapMoveListener.dataChange();
        };
        SceneManagerBase.prototype.resetAllRoles = function () {
            var s = this;
            var len;
            s.delSceneElement();
            len = s._sceneRoleVec.length;
            while (--len > -1) {
                s._sceneRoleVec[len].reset(s._sceneRoleVec[len].cfg);
            }
        };
        SceneManagerBase.prototype.setSceneElement = function (l, r, t, b, reset) {
            if (t === void 0) { t = 0; }
            if (b === void 0) { b = 1000; }
            if (reset === void 0) { reset = false; }
            var s = this;
            var size;
            var arr;
            var i, len, j, len2, k, len3;
            var key;
            var change1, change2;
            var mapName, backName, rowName;
            var mapX, mapY;
            var col, row, mapCol, mapRow, innerCol, innerRow; //图集行列索引，地图砖块行列索引，图集内部行列
            var back_mapY, back_mapX, back_r, back_l, back_col, back_mapCol, back_innerCol; //背景图集行列索引，地图砖块行列索引，图集内部行列
            if (s._sceneCfg == null)
                return;
            size = s._mapAreaSize;
            mapX = l;
            mapY = t;
            // offX = l % size;
            // offY = t % size;						
            back_mapY = t * s._backScaleY | 0;
            back_mapX = l * s._backScaleX | 0;
            back_l = Math.floor(back_mapX / size);
            back_r = back_l + r - l;
            l = Math.floor(l / size);
            r = Math.floor(r / size);
            t = Math.floor(t / size);
            b = Math.floor(b / size);
            change1 = l != s._visibleRect.left || r != s._visibleRect.right;
            change2 = t != s._visibleRect.top || b != s._visibleRect.bottom;
            mapName = s._mapName;
            backName = s._backMapName;
            s._mapBackBricker.clearBricks();
            if (backName) {
                len3 = back_r + 1;
                for (k = back_l; k < len3; ++k) {
                    back_col = k % s._maxAreaX;
                    back_col = back_col < 0 ? back_col + s._maxAreaX : back_col;
                    mapCol = back_col / s._mapPCol | 0;
                    innerCol = back_col % s._mapPCol;
                    key = mapName + "_" + mapCol + "_0";
                    s._mapBackBricker.brickDraw(k * size - back_mapX, -back_mapY, innerCol, 0, s.getBrickTexture(backName, mapCol, 0), key, size);
                }
            }
            s._mapBricker.clearBricks();
            var t1, t2;
            if (s._sceneCfg.areaRoles && (change1 || change2 || reset)) {
                // mapName = s._sceneCfg.sceneInfo.mapName;
                len = b + 1;
                len3 = r + 1;
                s._areaArr.length = 0;
                s._areaYArr.length = 0;
                for (i = t; i < len; ++i)
                    s._areaYArr.push(i);
                for (k = l; k < len3; ++k)
                    s._areaArr.push(k);
                for (i = t; i < len; ++i) {
                    row = i % s._maxAreaY;
                    row = row < 0 ? row + s._maxAreaY : row;
                    mapRow = row / s._mapPRow | 0;
                    innerRow = row % s._mapPRow;
                    for (k = l; k < len3; ++k) {
                        col = k % s._maxAreaX;
                        col = col < 0 ? col + s._maxAreaX : col;
                        mapCol = col / s._mapPCol | 0;
                        innerCol = col % s._mapPCol;
                        key = mapName + "_" + mapCol + "_" + mapRow;
                        s._mapBricker.brickDraw(k * size - mapX, i * size - mapY, innerCol, innerRow, s.getBrickTexture(mapName, mapCol, mapRow), key, size);
                        if (reset || k < s._visibleRect.left || k > s._visibleRect.right || i < s._visibleRect.top || i > s._visibleRect.bottom) {
                            key = mapName + "_" + row + "_" + col;
                            if (s._sceneCfg.areaRoles[key] == null)
                                continue;
                            arr = s._sceneCfg.areaRoles[key];
                            len2 = arr.length;
                            for (j = 0; j < len2; ++j) {
                                if (s._sceneCfg.sceneRoles[arr[j]])
                                    s.createRoleByCfg(s._sceneCfg.sceneRoles[arr[j]]);
                                else {
                                    Log.writeLog("找不到地图元素：" + arr[j] + "，区域：" + key);
                                }
                            }
                        }
                    }
                }
                s._visibleRect.left = l;
                s._visibleRect.right = r;
                s._visibleRect.top = t;
                s._visibleRect.bottom = b;
                s.delSceneElement();
                //				trace("bodys:"+GameManager.game.gameWorld.world.GetBodyCount()+"-"+LayerManager.getInstance().roadLay.numChildren,_visibleRect.left,_visibleRect.right,_areaArr,_areaYArr);
            }
            else {
                len = b + 1;
                len3 = r + 1;
                // len = t + 1;
                // len3 = l + 1;
                for (i = t; i < len; ++i) {
                    row = i % s._maxAreaY;
                    row = row < 0 ? row + s._maxAreaY : row;
                    mapRow = row / s._mapPRow | 0;
                    innerRow = row % s._mapPRow;
                    for (k = l; k < len3; ++k) {
                        col = k % s._maxAreaX;
                        col = col < 0 ? col + s._maxAreaX : col;
                        mapCol = col / s._mapPCol | 0;
                        innerCol = col % s._mapPCol;
                        key = mapName + "_" + mapCol + "_" + mapRow;
                        s._mapBricker.brickDraw(k * size - mapX, i * size - mapY, innerCol, innerRow, s.getBrickTexture(mapName, mapCol, mapRow), key, size);
                    }
                }
            }
        };
        /**获取地图切片纹理
         * @param mapName 地图图片名称
         * @param mapCol 列索引
         * @param mapRow 列索引
        */
        SceneManagerBase.prototype.getBrickTexture = function (mapName, mapCol, mapRow) {
            return Main.instance.getRes(mapName + "_" + mapCol + "_" + mapRow + ".png");
        };
        /**设置角色状态，当sts设置为-1时，清除状态为1的状态记录，为其他状态则不受影响，1表示已经出生 2表示永久死亡*/
        SceneManagerBase.prototype.setRoleSceneSts = function (id, sts) {
            if (sts === void 0) { sts = -1; }
            var s = this;
            if (id == null)
                return;
            if (sts == -1) {
                if (s._sceneRoleStsDict[id] == 1)
                    delete s._sceneRoleStsDict[id];
                return;
            }
            s._sceneRoleStsDict[id] = sts;
        };
        SceneManagerBase.prototype.createRoleByCfg = function (cfg) {
            var s = this;
            var r;
            return r;
        };
        SceneManagerBase.prototype.delSceneElement = function () {
            var s = this;
            var j, len2;
            var delRole;
            len2 = s._sceneRoleVec.length;
            if (len2 == 0)
                return;
            while (--len2 > -1) {
                delRole = s._sceneRoleVec[len2];
                if (s.isAreaInVisRect(delRole.areaRowMin, delRole.areaRowMax, delRole.areaColMin, delRole.areaColMax))
                    continue;
                delRole.clear();
                s._sceneRoleVec.splice(len2, 1);
            }
        };
        /**添加一个角色到场景，根据roleListType添加到场景不同的分组列表，默认只有-1这个分组，可定义不同编号分组，添加到对应数组，但是切记removeFromSceneVec方法里面也要进行重载，移除时根据角色的roleListType的类型从列表中相应移除
         * @param r 角色
         * @param type 类型
        */
        SceneManagerBase.prototype.addToSceneVec = function (r) {
            var s = this;
            var arr;
            if (-1 == r.roleListType) {
                s._sceneRoleVec.push(r);
            }
        };
        /**从场景的角色列表移除一个角色，默认列表是sceneRoleVec，roleListType类型-1，
         * @param r 角色
         * @param type 类型
        */
        SceneManagerBase.prototype.removeFromSceneVec = function (r) {
            var arr;
            var s = this;
            if (-1 == r.roleListType) {
                arr = s._sceneRoleVec;
                if (arr)
                    arr.splice(r.seq, 1);
            }
        };
        SceneManagerBase.prototype.findPath = function (fromX, fromY, toX, toY, trackArr, trackDirectionArr, speed) {
            var s = this;
            var gridSize;
            var agl, disX, disY;
            var stX, stY, edX, edY, dir;
            var i, len;
            var halfPI;
            trackDirectionArr.length = trackArr.length = 0;
            if (Math.abs(toX - fromX) < 11 && Math.abs(toY - fromY) < 11) {
                return trackArr;
            }
            gridSize = s.gridSize;
            var stCol, stRow, edCol, edRow;
            stCol = fromX / gridSize | 0;
            stRow = fromY / gridSize | 0;
            edCol = toX / gridSize | 0;
            edRow = toY / gridSize | 0;
            if (stCol == edCol && stRow == edRow) {
                trackArr.push([toX, toY]);
                if (toY == fromY)
                    trackDirectionArr.push(fromX <= toX ? map2D.DirectionType.RIGHT : map2D.DirectionType.LEFT);
                else if (toY < fromY) {
                    if (fromX == toX)
                        trackDirectionArr.push(map2D.DirectionType.UP);
                    else
                        trackDirectionArr.push(fromX < toX ? map2D.DirectionType.LEFT_UP : map2D.DirectionType.RIGHT_UP);
                }
                else if (toY > fromY) {
                    if (fromX == toX)
                        trackDirectionArr.push(map2D.DirectionType.DOWN);
                    else
                        trackDirectionArr.push(fromX < toX ? map2D.DirectionType.LEFT_DOWN : map2D.DirectionType.RIGHT_DOWN);
                }
                return;
            }
            var arr = [];
            halfPI = Math.PI / 2;
            trackArr = s.aStar.doSearch(fromX / gridSize | 0, fromY / gridSize | 0, toX / gridSize | 0, toY / gridSize | 0, trackArr);
            trackArr.reverse();
            len = trackArr.length;
            stX = fromX / gridSize | 0;
            stY = fromY / gridSize | 0;
            for (i = 0; i < len; ++i) {
                edX = trackArr[i][0];
                edY = trackArr[i][1];
                disX = edX - stX;
                disY = edY - stY;
                agl = disX == 0 ? (disY > 0 ? halfPI : -halfPI) : Math.atan2(disY, disX);
                stX = edX;
                stY = edY;
                dir = map2D.DirectionType.getDirectorSts(agl, map2D.DirectionMode.EIGHT);
                // if(dir == DirectionType.UP || dir == DirectionType.DOWN)
                // 	trackArr[i] = (trackArr[i][1] + 0.5) * gridSize;
                // else
                // 	trackArr[i] = (trackArr[i][0] + 0.5) * gridSize;
                trackArr[i][0] = (trackArr[i][0] + 0.5) * gridSize;
                trackArr[i][1] = (trackArr[i][1] + 0.5) * gridSize;
                trackDirectionArr.push(dir);
            }
            return trackArr;
        };
        /**判断区域坐标是否在可视范围内*/
        SceneManagerBase.prototype.isInVisRect = function (row, col) {
            var s = this;
            return s._areaArr[0] <= col && s._areaArr[s._areaArr.length - 1] >= col && s._areaYArr[0] <= row && s._areaYArr[s._areaYArr.length - 1] >= row;
        };
        /**判断区域是否在可视范围内*/
        SceneManagerBase.prototype.isAreaInVisRect = function (minRow, maxRow, minCol, maxCol) {
            var s = this;
            return !(s._areaArr[0] > maxCol || s._areaArr[s._areaArr.length - 1] < minCol || s._areaYArr[0] > maxRow || s._areaYArr[s._areaYArr.length - 1] < minRow);
        };
        SceneManagerBase.prototype.setScale = function (scale, check) {
            if (check === void 0) { check = true; }
            var s = this;
            if (scale == s._sceneScale && check)
                return;
            var sW, sH, sX, sY;
            var def = s.getDefault();
            sW = s._sceneWidth;
            sH = s._sceneHeight;
            s._sceneWidth = s._sceneBaseWidth / scale;
            s._sceneHeight = s._sceneBaseHeight / scale;
            s._sceneScale = scale;
            s._isScaleChange = true;
            s.setRunRect(s._sceneWidth * def.run_width_p, s._sceneWidth * def.run_width_p2, s._sceneHeight * def.run_height_p, s._sceneHeight * def.run_height_p2);
        };
        SceneManagerBase.prototype.setRunRect = function (l, r, t, b) {
            var s = this;
            s._runRect.left = l;
            s._runRect.right = r;
            s._runRect.top = t;
            s._runRect.bottom = b;
        };
        SceneManagerBase.prototype.setScaleTo = function (scale) {
            var s = this;
            if (GYLite.CommonUtil.loopTime - s._scaleTime < s._scaleKeepTime)
                return;
            if (s._targetScale == scale)
                return;
            if (s._targetScale == 0) {
                if (s._sceneScale == scale)
                    return;
                GYLite.CommonUtil.addStageLoop(s.scaleLoop, s);
            }
            s._scaleTime = GYLite.CommonUtil.loopTime;
            s._targetScale = scale;
            s._scaleDir = s._targetScale > s._sceneScale ? 1 : 0;
        };
        SceneManagerBase.prototype.stopScale = function (scl) {
            var s = this;
            s.setScale(scl);
            GYLite.CommonUtil.delStageLoop(s.scaleLoop, s);
            s._targetScale = 0;
        };
        SceneManagerBase.prototype.scaleLoop = function (t) {
            var s = this;
            if (s._scaleDir == 1) {
                if (s._sceneScale < s._targetScale)
                    s.setScale(s._sceneScale + s._scaleStep);
                else
                    s.stopScale(s._targetScale);
            }
            else {
                if (s._sceneScale > s._targetScale)
                    s.setScale(s._sceneScale - s._scaleStep);
                else
                    s.stopScale(s._targetScale);
            }
        };
        Object.defineProperty(SceneManagerBase.prototype, "sceneTouchabled", {
            /**场景是否可触碰寻路*/
            get: function () {
                return this._sceneTouchabled;
            },
            enumerable: false,
            configurable: true
        });
        SceneManagerBase.prototype.touchBegin = function (e) {
            var s = this;
            if (!s._sceneTouchabled)
                return;
            s._touchTarget = e.target;
            if (s._touchTarget == s._sceneLayer) {
                s._touchMapX = s._sceneLayer.mouseX;
                s._touchMapY = s._sceneLayer.mouseY;
                // GYLite.CommonUtil.addStageLoop(s.touchMapMove,s);
            }
        };
        SceneManagerBase.prototype.touchEnd = function (e) {
            var s = this;
            var toX, toY;
            if (!s._sceneTouchabled)
                return;
            toX = s._sceneLayer.mouseX;
            toY = s._sceneLayer.mouseY;
            if (s._touchTarget == s._sceneLayer) {
                // GYLite.CommonUtil.delStageLoop(s.touchMapMove,s);
                // if(!s._touchDrag)
                // {
                // 	//自动寻路到toX toY处
                // 	s._myPlayer.findPathTo(s._mapX + toX, s._mapY + toY);
                // }
                // else
                // 	s._myPlayer.setOperSts(StsType.STAND);
                s._myPlayer.setEndCoord(s._mapX + toX, s._mapY + toY);
            }
            s._touchDrag = false;
            s._touchTarget = null;
        };
        SceneManagerBase.prototype.touchMapMove = function (t) {
            var s = this;
            var toX, toY;
            toX = s._sceneLayer.mouseX;
            toY = s._sceneLayer.mouseY;
            if (Math.abs(toX - s._touchMapX) > 5 || Math.abs(toY - s._touchMapY) > 5) {
                if (s._myPlayer.directSts > 0)
                    return;
                var agl = void 0;
                agl = Math.atan2(toY - s._sceneHeight / 2, toX - s._sceneWidth / 2);
                s._myPlayer.moveDir(agl);
                s._touchDrag = true;
            }
        };
        /**移除角色，此方法角色内部调用，如需移除角色，请直接设置角色的idDel属性*/
        SceneManagerBase.prototype.addDelRole = function (r) {
            var s = this;
            s._delRoleVec.push(r);
        };
        SceneManagerBase.prototype.sortYIndex = function (a, b) {
            if (a.absY < b.absY)
                return -1;
            return 1;
        };
        /**排列层级*/
        SceneManagerBase.prototype.resetDp = function () {
            var s = this;
            // 默认列表
            var i, len;
            var r;
            len = s._sceneRoleVec.length;
            for (i = 0; i < len; ++i) {
                r = s._sceneRoleVec[i];
                if (r.isVisible())
                    s._depthArr.push(r);
            }
            if (s._depthArr.length > 0) {
                s._depthArr.sort(s.sortYIndex);
                var len_1;
                len_1 = s._depthArr.length;
                while (--len_1 > -1)
                    s._depthArr[len_1].setYIndex(0);
                s._depthArr.length = 0;
            }
        };
        SceneManagerBase.prototype.reloadScene = function () {
            var s = this;
            s.loadScene(s._sceneCfg);
        };
        SceneManagerBase.prototype.loadScene = function (cfg) {
            if (cfg === void 0) { cfg = null; }
            var s = this;
            s.clearScene();
            s.createScene(cfg);
            s.enterScene();
        };
        SceneManagerBase.prototype.createScene = function (sceneCfg) {
            var s = this;
            var i, len;
            if (sceneCfg)
                s._sceneCfg = sceneCfg;
            s.configSet(s._sceneCfg);
            s.setMapXY(s._initMapX, s._initMapY, true);
        };
        SceneManagerBase.prototype.outputCfg = function () {
        };
        SceneManagerBase.prototype.createMapJson = function (fileName, resKey) {
            if (resKey === void 0) { resKey = null; }
            var json;
            var i, len, j, len2;
            var s = this;
            var arr;
            var n, m;
            var stX, stY, w, h;
            json = { "file": fileName + ".png", "frames": {} };
            //i 列 j 行
            len = s._maxAreaX;
            len2 = s._maxAreaY;
            arr = fileName.split("_");
            n = Number(arr[1]); //n 大区-列
            m = Number(arr[2]); //m 大区-行
            stX = 0;
            stY = 0;
            w = h = s._mapAreaSize;
            for (i = 0; i < len; ++i) {
                stX = i * w;
                for (j = 0; j < len2; ++j) {
                    stY = j * h;
                    json.frames[i + "_" + j] = { "x": stX, "y": stY, "w": w, "h": h, "offX": 0, "offY": 0, "sourceW": w, "sourceH": h };
                }
            }
            if (resKey)
                GYLite.GYLoader.setDataRes(resKey + fileName, json);
        };
        SceneManagerBase.prototype.enterScene = function () {
            var s = this;
        };
        SceneManagerBase.prototype.addToDepthList = function (list) {
            var i, len;
            var r;
            var s = this;
            len = list.length;
            for (i = 0; i < len; ++i) {
                r = list[i];
                if (r.isVisible())
                    s._depthArr.push(r);
            }
        };
        SceneManagerBase.prototype.listLoop = function (t, list) {
            var i, len;
            var r;
            len = list.length;
            for (i = 0; i < len; ++i) {
                r = list[i];
                r.loop(t, i);
            }
        };
        SceneManagerBase.prototype.clearList = function (list) {
            var len;
            len = list.length;
            while (--len > -1)
                list[len].clear();
            list.length = 0;
        };
        /**清理场景，其他东西请在清理方法执行前进行清理*/
        SceneManagerBase.prototype.clearScene = function () {
            var s = this;
            s.clearList(s._sceneRoleVec);
            s._sceneRoleStsDict = {};
            // s._myPlayer.clear();
            s._mapX = s._mapY = -1;
            s.stopScale(1);
            s._areaArr.length = s._visibleRect.left = s._visibleRect.right = s._visibleRect.top = s._visibleRect.bottom = 0;
        };
        /**配置设置*/
        SceneManagerBase.prototype.configSet = function (sceneCfg) {
            var s = this;
            var cfg = sceneCfg.sceneInfo;
            var def;
            s._rateCount = 0;
            s._mapPW = sceneCfg.atalsWidth ? sceneCfg.atalsWidth : 2048;
            s._mapPH = sceneCfg.atalsHeight ? sceneCfg.atalsHeight : 2048;
            def = s.getDefault();
            s._backScaleX = cfg.backScaleX == null ? def.back_scale_x : cfg.backScaleX;
            s._backScaleY = cfg.backScaleY == null ? def.back_scale_y : cfg.backScaleY;
            s._maxAreaX = cfg.maxAreaX == null ? def.max_area_x : cfg.maxAreaX;
            s._maxAreaY = cfg.maxAreaY == null ? def.max_area_y : cfg.maxAreaY;
            s._mapAreaSize = cfg.mapAreaSize == null ? def.map_areasize : cfg.mapAreaSize;
            s._mapName = cfg.mapName;
            s._backMapName = cfg.backMapName;
            s._initMapX = cfg.mapInitX == null ? def.map_init_x : cfg.mapInitX;
            s._initMapY = cfg.mapInitY == null ? def.map_init_y : cfg.mapInitY;
            s._mapPCol = s._mapPW / s._mapAreaSize;
            s._mapPRow = s._mapPH / s._mapAreaSize;
            s._mapMinX = 0;
            s._mapMinY = 0;
            s._mapMaxX = cfg.mapMaxX > 0 ? cfg.mapMaxX : (s._maxAreaX * s._mapAreaSize);
            s._mapMaxY = cfg.mapMaxY > 0 ? cfg.mapMaxY : (s._maxAreaY * s._mapAreaSize);
            s._gridSize = cfg.gridSize == null ? def.grid_size : cfg.gridSize;
            if (sceneCfg.barriers == null) {
                sceneCfg.barriers = [];
                sceneCfg.barriers.length = s.mapGridCol * s.mapGridRow;
                sceneCfg.barriers.fill(map2D.Barrier.LAND);
            }
            s.barrier.setBarrier(sceneCfg.barriers, [s._mapMinX, s._mapMinY, s._mapMaxX, s._mapMaxY, s.mapGridCol, s.mapGridRow], s._gridSize);
            if (s.aStar)
                s.aStar.dispose();
            s.aStar = new map2D.AStar(s, false);
        };
        SceneManagerBase.prototype.loop = function (t) {
            var i, len;
            var r;
            var s = this;
            s.timeTick = t;
            ++s._rateCount;
            s.enterLoop(t);
            s.exitLoop(t);
        };
        SceneManagerBase.prototype.exitLoop = function (t) {
            var s = this;
            s._lastTick = t;
        };
        SceneManagerBase.prototype.enterLoop = function (t) {
            var s = this;
            var i, len;
            len = s._delRoleVec.length;
            if (len > 0) {
                while (--len > -1) {
                    s._delRoleVec[len].clear();
                    s.removeFromSceneVec(s._delRoleVec[len]);
                }
                s._delRoleVec.length = 0;
            }
            s.setMoveRect(s._myPlayer);
            var indexFlag;
            indexFlag = t - s._yIndexTime > 100;
            if (indexFlag) {
                s.resetDp();
                s._yIndexTime = t;
            }
            s.listLoop(t, s._sceneRoleVec);
        };
        Object.defineProperty(SceneManagerBase.prototype, "rateCount", {
            get: function () {
                return this._rateCount;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneID", {
            get: function () {
                return this._sceneCfg.sceneID;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneLayer", {
            get: function () {
                return this._sceneLayer;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "gameContainer", {
            get: function () {
                return this._gameContainer;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapX", {
            get: function () {
                var s = this;
                return s._mapX;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapY", {
            get: function () {
                var s = this;
                return s._mapY;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "gridSize", {
            get: function () {
                return this._gridSize;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapGridCol", {
            get: function () {
                var s = this;
                return Math.ceil(s._mapMaxX / s._gridSize);
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapGridRow", {
            get: function () {
                var s = this;
                return Math.ceil(s._mapMaxY / s._gridSize);
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneRoleVec", {
            get: function () {
                var s = this;
                return s._sceneRoleVec;
            },
            enumerable: false,
            configurable: true
        });
        SceneManagerBase.prototype.setMoveRect = function (r) {
            var s = this;
            var localX, localY;
            var moveX, moveY;
            var mX, mY;
            var rX, rY;
            if (r == null)
                return;
            rX = r.absX >> 0;
            rY = r.absY >> 0;
            localX = rX - s._mapX;
            localY = rY - s._mapY;
            if (localX > s._runRect.right) {
                moveX = rX - s._runRect.right;
                mX = (moveX > s._mapMaxX - s._sceneWidth) ? s._mapMaxX - s._sceneWidth : moveX;
            }
            else if (localX < s._runRect.left) {
                moveX = rX - s._runRect.left;
                mX = moveX < s._mapMinX ? s._mapMinX : moveX;
            }
            else
                mX = s._mapX;
            if (localY > s._runRect.bottom) {
                moveY = rY - s._runRect.bottom;
                mY = (moveY > s._mapMaxY - s._sceneHeight) ? s._mapMaxY - s._sceneHeight : moveY;
            }
            else if (localY < s._runRect.top) {
                moveY = rY - s._runRect.top;
                mY = moveY < s._mapMinY ? s._mapMinY : moveY;
            }
            else
                mY = s._mapY;
            s.setMapXY(mX | 0, mY | 0);
        };
        SceneManagerBase.prototype.findRole = function (rid) {
            var s = this;
            var len;
            len = s._sceneRoleVec.length;
            while (--len > -1) {
                if (s._sceneRoleVec[len].roleID == rid)
                    return s._sceneRoleVec[len];
            }
            return null;
        };
        /**是否可通行
         * @param x 世界坐标x
         * @param y 世界坐标y
        */
        SceneManagerBase.prototype.canPass = function (x, y) {
            var barrierState;
            var s = this;
            barrierState = s.barrier.barrierState(x, y);
            return barrierState & map2D.Barrier.PASS;
        };
        /**是否可通行(根据格子索引)
         * @param col 格子列索引
         * @param row 格子行索引
        */
        SceneManagerBase.prototype.canGridPass = function (col, row) {
            var barrierState;
            var s = this;
            barrierState = s.barrier.barrierGridState(col, row);
            return barrierState & map2D.Barrier.PASS;
        };
        /**是否障碍(根据世界坐标)
         * @param x
         * @param y
        */
        SceneManagerBase.prototype.isBarrier = function (x, y) {
            var s = this;
            var toX, toY;
            var gridSize;
            gridSize = s.gridSize;
            toX = x / gridSize | 0;
            toY = y / gridSize | 0;
            return !s.canGridPass(toX, toY);
        };
        Object.defineProperty(SceneManagerBase.prototype, "mapName", {
            get: function () {
                return this._mapName;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapAreaSize", {
            get: function () {
                return this._mapAreaSize;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "myPlayer", {
            get: function () {
                return this._myPlayer;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapMoveListener", {
            get: function () {
                return this._mapMoveListener;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneCfg", {
            get: function () {
                return this._sceneCfg;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "maxAreaX", {
            /**X方向尽头区号*/
            get: function () {
                return this._maxAreaX;
            },
            set: function (value) {
                this._maxAreaX = value;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "maxAreaY", {
            /**Y方向尽头区号*/
            get: function () {
                return this._maxAreaY;
            },
            set: function (value) {
                this._maxAreaY = value;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapMinX", {
            get: function () {
                return this._mapMinX;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapMinY", {
            get: function () {
                return this._mapMinY;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapMaxX", {
            get: function () {
                return this._mapMaxX;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapMaxY", {
            get: function () {
                return this._mapMaxY;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneScale", {
            // public set mapAreaSize(value:number)
            // {
            // 	this._mapAreaSize = value;
            // }
            get: function () {
                return this._sceneScale;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneHeight", {
            get: function () {
                return this._sceneHeight;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "sceneWidth", {
            get: function () {
                return this._sceneWidth;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapRight", {
            get: function () {
                return this._mapRight;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "mapBottom", {
            get: function () {
                return this._mapBottom;
            },
            enumerable: false,
            configurable: true
        });
        SceneManagerBase.prototype.keyFocus = function () {
            return true;
        };
        SceneManagerBase.prototype.kDown = function (keyCode) {
        };
        SceneManagerBase.prototype.kUp = function (keyCode) {
        };
        /**键控方向的判断
         * @return 返回方向
        */
        SceneManagerBase.prototype.keyDirect = function () {
            var direct;
            var s = this;
            var keyBoard = GYLite.GYKeyboard.getInstance();
            if (keyBoard.isKeyDown(GYLite.Keyboard.W)) {
                if (keyBoard.isKeyDown(GYLite.Keyboard.D))
                    direct = map2D.DirectionType.RIGHT_UP;
                else if (keyBoard.isKeyDown(GYLite.Keyboard.A))
                    direct = map2D.DirectionType.LEFT_UP;
                else
                    direct = map2D.DirectionType.UP;
            }
            else if (keyBoard.isKeyDown(GYLite.Keyboard.D)) {
                if (keyBoard.isKeyDown(GYLite.Keyboard.S))
                    direct = map2D.DirectionType.RIGHT_DOWN;
                else
                    direct = map2D.DirectionType.RIGHT;
            }
            else if (keyBoard.isKeyDown(GYLite.Keyboard.S)) {
                if (keyBoard.isKeyDown(GYLite.Keyboard.A))
                    direct = map2D.DirectionType.LEFT_DOWN;
                else
                    direct = map2D.DirectionType.DOWN;
            }
            else if (keyBoard.isKeyDown(GYLite.Keyboard.A)) {
                direct = map2D.DirectionType.LEFT;
            }
            else {
                direct = -1;
            }
            return direct;
        };
        /**开启场景编辑，编辑时，不可点击寻路*/
        SceneManagerBase.prototype.editable = function (val) {
            var s = this;
            if (s._editabled == val)
                return;
            s._editabled = val;
            s._sceneTouchabled = !s._editabled;
        };
        Object.defineProperty(SceneManagerBase.prototype, "editabled", {
            get: function () {
                return this._editabled;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(SceneManagerBase.prototype, "disposed", {
            get: function () {
                return this._disposed;
            },
            enumerable: false,
            configurable: true
        });
        SceneManagerBase.prototype.dispose = function () {
            var s = this;
            if (s._disposed)
                return;
            s._disposed = true;
            GYLite.GYKeyboard.getInstance().removeKeyListener(this);
        };
        return SceneManagerBase;
    }());
    map2D.SceneManagerBase = SceneManagerBase;
})(map2D || (map2D = {}));
__reflect(map2D.SceneManagerBase.prototype, "map2D.SceneManagerBase", ["map2D.IScene", "GYLite.IKeyBoardObject"]);

var map2D;
(function (map2D) {
    var ANode = /** @class */ (function () {
        function ANode(pos, id, block, pid) {
            if (block === void 0) { block = 0; }
            if (pid === void 0) { pid = 0; }
            var s = this;
            s.id = id;
            s.pid = pid;
            s.pos = pos;
            s.block = block;
            s.G = 0;
            s.H = 0;
            s.F = 0;
        }
        ANode.prototype.clone = function () {
            var s = this;
            return ANode.createANode([s.pos[0], s.pos[1]], s.id, s.block, s.pid);
        };
        ANode.prototype.clear = function () {
            var s = this;
            if (s.inPool)
                return;
            GYLite.PoolUtil.toPool(s, ANode);
        };
        ANode.prototype.outPoolInit = function () {
            var s = this;
            s.G = 0;
            s.H = 0;
            s.F = 0;
        };
        ANode.prototype.dispose = function () {
        };
        ANode.createANode = function (pos, id, block, pid) {
            if (block === void 0) { block = 0; }
            if (pid === void 0) { pid = 0; }
            var aNode;
            aNode = GYLite.PoolUtil.fromPool(ANode);
            aNode.pos = pos;
            aNode.id = id;
            aNode.block = block;
            aNode.pid = pid;
            return aNode;
        };
        return ANode;
    }());
    map2D.ANode = ANode;
})(map2D || (map2D = {}));
__reflect(map2D.ANode.prototype, "map2D.ANode", ["GYLite.IPoolObject"]);

var map2D;
(function (map2D) {
    /*
     * A* 寻路算法
     *
     * @author Dingo
     * @version 1.0
     * @date 070831
     */
    var AStar = /** @class */ (function () {
        //构造函数
        function AStar(scene, debug) {
            if (debug === void 0) { debug = false; }
            //垂直或水平移动一格的代价
            this.COST_STRAIGHT = 10;
            //斜向移动一格的代价
            this.COST_DIAGONAL = 14;
            //逆向偏离目标的代价
            this.COST_INSERVE = 80;
            this._listenId = -1;
            var s = this;
            s._nMaxTry = 600;
            s._nCrtTry = 0;
            s._aOpenList = [];
            s._aCloseList = [];
            s._mapWidth = scene.mapGridCol; //amap[0].length;
            s._mapHeight = scene.mapGridRow; //amap.length;
            s._scene = scene;
            s.canDiaonal = true;
            s._aTryList = [];
            s._debug = debug;
            // s.aNodeMap = amap;
        }
        //添加到open表
        // public addFirstOpen():void{
        // 	let s = this;
        // 	s.aOpenList.push(s.ndStart);
        // }
        //取得指定点周围可通过的点，从正上方开始
        AStar.prototype.getRound = function (apos) {
            var s = this;
            var arr = [];
            for (var i = 0; i < AStar.aDir.length; i++) {
                var xp = apos[0] + AStar.aDir[i][0];
                var yp = apos[1] + AStar.aDir[i][1];
                if (s.isStart([xp, yp]) || !s.isPass([xp, yp]) || s.isCorner([xp, yp]) || s.isInClose([xp, yp]))
                    continue;
                arr.push([xp, yp]);
            }
            return arr;
        };
        // //是否超出地图范围
        // private isOutRange(apos:any[]):boolean{
        // 	let s = this;
        // 	if(apos[0]<0 || apos[0] >= s.mapwidth || apos[1] < 0 || apos[1]>= s.mapheight)
        // 		return true;
        // 	return false;
        // }		
        //是否是起点
        AStar.prototype.isStart = function (apos) {
            var s = this;
            if (apos[0] == s._ndStart.pos[0] && apos[1] == s._ndStart.pos[1])
                return true;
            return false;
        };
        // //是否可以通过		
        AStar.prototype.isPass = function (apos) {
            var s = this;
            // if(s.isOutRange(apos)){
            // 	return false;
            // }else{	
            // 	return (s.aNodeMap[apos[1]][apos[0]]>0?false:true);
            // }
            return s._scene.canGridPass(apos[0], apos[1]) > 0;
        };
        //是否不可通行的拐角
        AStar.prototype.isCorner = function (apos) {
            var s = this;
            if (s.isPass(apos)) {
                if (apos[0] > s._ndCurrent.pos[0]) {
                    if (apos[1] > s._ndCurrent.pos[1]) {
                        if (!s.canDiaonal)
                            return true;
                        if (!s.isPass([apos[0] - 1, apos[1]]) || !s.isPass([apos[0], apos[1] - 1]))
                            return true;
                    }
                    else if (apos[1] < s._ndCurrent.pos[1]) {
                        if (!s.canDiaonal)
                            return true;
                        if (!s.isPass([apos[0] - 1, apos[1]]) || !s.isPass([apos[0], apos[1] + 1]))
                            return true;
                    }
                }
                else if (apos[0] < s._ndCurrent.pos[0]) {
                    if (apos[1] > s._ndCurrent.pos[1]) {
                        if (!s.canDiaonal)
                            return true;
                        if (!s.isPass([apos[0] + 1, apos[1]]) || !s.isPass([apos[0], apos[1] - 1]))
                            return true;
                    }
                    else if (apos[1] < s._ndCurrent.pos[1]) {
                        if (!s.canDiaonal)
                            return true;
                        if (!s.isPass([apos[0] + 1, apos[1]]) || !s.isPass([apos[0], apos[1] + 1]))
                            return true;
                    }
                }
            }
            return false;
        };
        //是否在开启列表中
        //获得传入参数在aOpenlist数组的位置，如不存在返回false，存在为true，位置索引保存到变量num中。
        AStar.prototype.isInOpen = function (apos) {
            var s = this;
            // var bool:boolean = false;
            var id = apos[1] * s._mapWidth + apos[0];
            for (var i = 0; i < s._aOpenList.length; i++) {
                if (s._aOpenList[i].id == id) {
                    // bool = true;
                    // s._num = i;
                    return i;
                    // break;
                }
            }
            // return bool;
            return -1;
        };
        //是否在关闭列表中
        AStar.prototype.isInClose = function (apos) {
            var s = this;
            var bool = false;
            var id = apos[1] * s._mapWidth + apos[0];
            for (var i = 0; i < s._aCloseList.length; i++) {
                if (s._aCloseList[i].id == id) {
                    bool = true;
                    break;
                }
            }
            return bool;
        };
        //取得F值，参数为某一节点周围的节点
        AStar.prototype.getF = function (around) {
            //f,综合的距离值；
            //h,给定节点到目标点的距离值；
            //g,起点到给定节点的距离值
            var f, g, h;
            var s = this;
            var apos;
            for (var i = 0; i < around.length; i++) {
                apos = around[i];
                //是否与起点在同一直线上
                if (apos[0] == s._ndStart.pos[0] || apos[1] == s._ndStart.pos[1]) {
                    g = s._ndCurrent.G + s.COST_STRAIGHT;
                }
                else {
                    g = s._ndCurrent.G + s.COST_DIAGONAL;
                }
                //如果当前点已存在aOpenlist数组中
                var ind = s.isInOpen(apos);
                if (ind > -1) {
                    var opos = s._aOpenList[ind];
                    //如果当前点G值更小，更改父节点
                    if (g < opos.G) {
                        opos.F = g + opos.H;
                        opos.G = g;
                        opos.pid = s._ndCurrent.id;
                    }
                    else {
                        g = opos.G;
                    }
                }
                //否则将当前点添加到aOpenList数组
                else {
                    h = s.calculateH(s._ndCurrent.pos, apos, s._ndEnd.pos); //(Math.abs(s._ndEnd.pos[0] - apos[0]) + Math.abs(s._ndEnd.pos[1] - apos[1])) * s.COST_STRAIGHT;//估值
                    f = g + h;
                    var newnode = map2D.ANode.createANode(apos, apos[1] * s._mapWidth + apos[0], 0, s._ndCurrent.id); //new ANode(apos,apos[1] * s.mapWidth + apos[0],0,s.ndCurrent.id);
                    newnode.F = f;
                    newnode.G = g;
                    newnode.H = h;
                    s._aOpenList.push(newnode);
                }
            }
        };
        //估值
        AStar.prototype.calculateH = function (curPos, nextPos, endPos) {
            var s = this;
            var dx, dy, costAdd = 0;
            dx = endPos[0] - nextPos[0];
            dy = endPos[1] - nextPos[1];
            if (curPos[0] < endPos[0] && nextPos[0] < curPos[0]) {
                costAdd += s.COST_INSERVE * (endPos[0] - curPos[0]);
            }
            else if (curPos[0] > endPos[0] && nextPos[0] > curPos[0]) {
                costAdd += s.COST_INSERVE * (curPos[0] - endPos[0]);
            }
            if (curPos[1] < endPos[1] && nextPos[1] < curPos[1]) {
                costAdd += s.COST_INSERVE * (endPos[1] - curPos[1]);
            }
            else if (curPos[1] > endPos[1] && nextPos[1] > curPos[1]) {
                costAdd += s.COST_INSERVE * (curPos[1] - endPos[1]);
            }
            if (dx < 0)
                dx = -dx;
            if (dy < 0)
                dy = -dy;
            return (dx + dy) * s.COST_STRAIGHT + costAdd; //(Math.abs(endPos[0] - nextPos[0]) + Math.abs(endPos[1] - nextPos[1])) * s.COST_STRAIGHT
        };
        AStar.prototype.sortF = function (a, b) {
            return a.F > b.F ? -1 : 1;
        };
        /**搜索路径
         * @param col 列索引
         * @param row 行索引
         * @param trackArr 寻路返回的结果数组，不传则创建一个新数组
         * @return 如果寻路成功，返回number[][] [[索引x，索引y]……]，失败返回null
         * */
        AStar.prototype.doSearch = function (startCol, startRow, endCol, endRow, trackArr) {
            if (trackArr === void 0) { trackArr = null; }
            var s = this;
            var apath;
            var t;
            apath = trackArr;
            if (s._debug)
                t = Date.now();
            s.clearANodes();
            // s.addFirstOpen();
            s._aTryList.length = 0;
            s._ndCurrent = s._ndStart = map2D.ANode.createANode([startCol, startRow], startCol + startRow * s._mapWidth);
            s._ndEnd = map2D.ANode.createANode([endCol, endRow], endCol + endRow * s._mapWidth);
            s._aOpenList.push(s._ndStart.clone());
            while (s._aOpenList.length) {
                s._nCrtTry++;
                //如果超出寻路步数限制
                if (s._nCrtTry > s._nMaxTry) {
                    s.destroyData();
                    return apath;
                }
                s._aTryList.push(s._ndCurrent);
                s.getF(s.getRound(s._ndCurrent.pos));
                //按照F值由大到小的顺序排列开启列表
                s._aOpenList.sort(s.sortF);
                //将开启列表最后一位元素列入关闭列表
                var lastNode = s._aOpenList[s._aOpenList.length - 1];
                s._aCloseList.push(lastNode);
                s._ndCurrent = lastNode;
                if (s._aOpenList.length > 1)
                    s._aOpenList.pop();
                //如果当前节点是目标节点，路径找到，返回true
                if (s._ndCurrent.id == s._ndEnd.id) {
                    apath = s.getPath(apath);
                    s.destroyData();
                    // s.bPathFind = true;
                    s._ndStart = s._ndCurrent;
                    apath = apath.length > 0 ? apath : null;
                    break;
                }
            }
            // s.bPathFind = false;
            s.destroyData();
            // s.aPath = [];
            if (s._debug) {
                console.log("本次寻路耗时：" + (Date.now() - t), "步数：" + s._aTryList.length);
                s.drawTestPath(apath, s._aTryList);
            }
            return apath;
        };
        Object.defineProperty(AStar.prototype, "tryList", {
            get: function () {
                return this._aTryList;
            },
            enumerable: false,
            configurable: true
        });
        //清空各数组
        AStar.prototype.destroyData = function () {
            var s = this;
            s.clearANodes();
            s._nCrtTry = 0;
        };
        /**取得路径数组
         * @param trackArr 寻路返回的结果数组
        */
        AStar.prototype.getPath = function (trackArr) {
            var s = this;
            var apath = trackArr ? trackArr : [];
            var tmpnode = s._aCloseList[s._aCloseList.length - 1];
            apath.push(tmpnode.pos);
            var inc = 0;
            while (inc <= s._aCloseList.length) {
                inc++;
                for (var i = 0; i < s._aCloseList.length; i++) {
                    if (s._aCloseList[i].id == tmpnode.pid) {
                        tmpnode = s._aCloseList[i];
                        apath.push(tmpnode.pos);
                    }
                    if (tmpnode.id == s._ndStart.id)
                        break;
                }
            }
            apath.pop();
            return apath;
        };
        AStar.prototype.dataChange = function (listenId) {
            var s = this;
            if (s._listenId == listenId) {
                if (s.testLay) {
                    s.testLay.x = -s._scene.mapX;
                    s.testLay.y = -s._scene.mapY;
                }
            }
        };
        AStar.prototype.drawTestPath = function (arr, tryList) {
            var i, len;
            var g;
            var s = this;
            var gridSize;
            if (!s._debug)
                return;
            if (s.testLay == null) {
                s.testLay = new GYLite.GYSprite;
                s.testLay.mouseThrough = true;
                s._scene.sceneLayer.addElement(s.testLay);
                s._listenId = s._scene.mapMoveListener.addListener(s);
            }
            gridSize = s._scene.gridSize;
            g = s.testLay.graphics;
            g.clear();
            if (arr && arr.length > 0) {
                len = arr.length;
                for (i = 0; i < len; ++i) {
                    g.beginFill(0xff0000, 0.5);
                    g.drawRect(arr[i][0] * gridSize + 1, arr[i][1] * gridSize + 1, gridSize - 2, gridSize - 2);
                }
                g.endFill();
            }
            if (tryList && tryList.length > 0) {
                len = tryList.length;
                for (i = 0; i < len; ++i) {
                    g.beginFill(0x0000ff, 0.3);
                    g.drawRect(tryList[i].pos[0] * gridSize + 4, tryList[i].pos[1] * gridSize + 4, gridSize - 8, gridSize - 8);
                }
                g.endFill();
            }
        };
        AStar.prototype.clearANodes = function () {
            var s = this;
            var len;
            if (s._ndStart) {
                s._ndStart.clear();
                s._ndStart = null;
            }
            if (s._ndStart) {
                s._ndEnd.clear();
                s._ndEnd = null;
            }
            len = s._aOpenList.length;
            while (--len > -1) {
                s._aOpenList[len].clear();
            }
            len = s._aCloseList.length;
            while (--len > -1) {
                s._aCloseList[len].clear();
            }
            s._aOpenList.length = 0;
            s._aCloseList.length = 0;
        };
        AStar.prototype.dispose = function () {
            var s = this;
            if (s.testLay)
                s.testLay.dispose();
            if (s._listenId > -1)
                s._scene.mapMoveListener.removeListener(s);
            s.clearANodes();
            s._aTryList.length = 0;
        };
        //八个方向数组，从节点正上方开始，顺时针排列
        AStar.aDir = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]];
        return AStar;
    }());
    map2D.AStar = AStar;
})(map2D || (map2D = {}));
__reflect(map2D.AStar.prototype, "map2D.AStar", ["GYLite.IListener"]);



